package com.king.oa.domain;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import com.opensymphony.xwork2.ActionContext;

/**
 * 用户
 * @author KingSir
 *
 */
@SuppressWarnings("unchecked")
public class User implements Serializable {
	
	private static final long serialVersionUID = 4214181533528070455L;
	
	private Long id;
	private String loginName;//登录名
	private String name;//姓名
	private String gender;//性别
	private String phoneNumber;//电话
	private String email;//邮箱
	private String password;//密码
	private String description;//个人说明
	private Department department;//隶属部门
	private Set<Role> roles = new HashSet<>();//身兼岗位

	/**
	 * 检测本用户是否有指定功能名称的权限
	 * @param name
	 * @return
	 */
	public boolean hasPrivilegeByName(String name){
		//超级管理员有所有权限
		if(isAdmin())
			return true;
			
		//普通用户判断是否有权限
		for(Role role: roles){
			for(Privilege privilege : role.getPrivileges()){
				if(privilege.getName().equals(name))
					return true;
			}
		}
		return false;
	}
	
	/**
	 * 检测本用户是否有指定功能Url的权限
	 * @param name
	 * @return
	 */
	public boolean hasPrivilegeByUrl(String privUrl){
		//超级管理员有所有权限
		if(isAdmin())
			return true;
		
		// >> 去掉后面的参数跟UI后缀,前面加斜杆
    	if(privUrl.contains("?")){
    		privUrl = privUrl.substring(0, privUrl.indexOf("?"));
    	}
    	
    	if(privUrl.endsWith("UI")){
    		privUrl = privUrl.substring(0, privUrl.length() - 2);
    	}
    	
    	if(!privUrl.startsWith("/")){
    		privUrl = "/" + privUrl;
    	}
    	
    	
    	//基本权限不需要控制
    	Collection<String> aliPrivileges = (Collection<String>) ActionContext
    			.getContext().getApplication().get("allPrivileges");
    	if(!aliPrivileges.contains(privUrl)){
    		return true;
    	}
    	
    	
		//普通用户判断是否有权限
		for(Role role: roles){
			for(Privilege privilege : role.getPrivileges()){
				if(privUrl.equals(privilege.getUrl()))
					return true;
			}
		}
		return false;
	}
	
	/**
	 * 判断本用户是否是超级管理员
	 * 
	 * @return
	 */
	public boolean isAdmin(){
		return "admin".equals(loginName);
	}
	
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
